function request(url, data) {
  return new Promise(function (resolve, reject) {
    business.request({
      url: url,
      data: data,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        resolve(res.data);
      },
      fail: function (err) {
        reject(err)
      }
    })
  });
}

function formatDate(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

function formatTime(date) {
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getTodayDay(date) {
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  return year + "-" + month + "-" + day;
}

function len(obj) {
  if (Array.isArray(obj)) {
    return obj.length;
  } else if (typeof obj === 'object') {
    return Object.keys(obj).length;
  }
  return 0;
}

module.exports = {
  request: request,
  formatDate: formatDate,
  formatTime: formatTime,
  getTodayDay: getTodayDay,
  len: len
}